package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdchannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdchannelService {

    @Resource
    private AdChannelMapper adChannelMapper;

    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        //1、判断channelDto是否为空
        if (channelDto==null){
            //如果dto为空，则返回无效参数的状态信息
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数检查
        channelDto.checkParam();
        //根据套餐来进行查询
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        //不为null是增加条件
        queryWrapper.like(channelDto.getName()!=null, AdChannel::getName,channelDto.getName());//分页
        Page page = new Page(channelDto.getPage(),channelDto.getSize());
        //分页查询
        IPage selectPage = adChannelMapper.selectPage(page, queryWrapper);
        //结果封装
        PageResponseResult responseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) selectPage.getTotal());
        //封装数据
        responseResult.setData(selectPage.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1、判断id是否存在
            if (id==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            //2、判断是否存在和状态是否使用
            AdChannel channel = getById(id);
            if (channel==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            if (channel.getStatus()){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
            //3、执行删除
            removeById(id);
            //4、返回结果
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult saveCheannel(AdChannel adChannel) {
        //1、判断参数是否存在
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置创建时间的参数
        adChannel.setCreatedTime(new Date());
        //保存
        save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1、判断参数是否存在
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public AdChannel findById(Integer id) {
        return getById(id);
    }
}



